package com.zimmur.solr;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.SortClause;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.FacetParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.zimmur.solr.common.Facet;
import com.zimmur.solr.common.SolrData;




@Service
public class SolrUtil<T> {
	Logger log=LoggerFactory.getLogger(SolrUtil.class);
	@Resource
	private SolrClient solrClient;
	
	/**
	 * search 
	 * @param q    		查询条件 *:* (AND/OR) *:*
	 * @param fq   		查询条件 *:* (AND/OR) *:*
	 * @param value 	排序条件 
	 * @param pageSize	结果条数
	 * @param pageNo	页码
	 * @param collection集合名称
	 * @return
	 * @throws SolrServerException
	 * @throws IOException
	 */
	public QueryResponse query(String q,String fq[],List<SortClause> value,int pageNo,String collection) throws SolrServerException, IOException{
        QueryResponse queryResponse;
        SolrQuery query = new SolrQuery();
        query.setQuery(q).setFilterQueries(fq).setSorts(value).setRows(SolrData.QUERY_ROWS).setStart((pageNo-1)*SolrData.QUERY_ROWS);
        queryResponse=solrClient.query(collection, query);
        return queryResponse;
	}
	/**
	 * 
	* @Title: query 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param q
	* @param fq
	* @param value
	* @param pageSize
	* @param pageNo
	* @param collection
	* @return
	* @throws SolrServerException
	* @throws IOException  
	* @return QueryResponse    返回类型
	 */
	public QueryResponse query(String q, String fq[], List<SortClause> value, int pageSize, int pageNo,
			Facet facet,String collection) throws SolrServerException, IOException {
		if (pageSize == 0) {
			pageSize = SolrData.QUERY_ROWS;
		}
		QueryResponse queryResponse;
		SolrQuery query = new SolrQuery();
		query.setQuery(q).setFilterQueries(fq).setSorts(value).setRows(pageSize).setStart((pageNo - 1) * pageSize);
		
		if (facet != null && facet.isStatus()) {
			query.setFacet(true);
			query.setParam(FacetParams.FACET_FIELD, facet.getFields());
		}
		queryResponse = solrClient.query(collection, query);
		return queryResponse;
	}
	public QueryResponse query(String q,String fq[],String orderByClause,int pageNo,String collection) throws SolrServerException, IOException{
        QueryResponse queryResponse;
        SolrQuery query = new SolrQuery();
        query.setQuery(q).setFilterQueries(fq).setRows(SolrData.QUERY_ROWS).setStart((pageNo-1)*SolrData.QUERY_ROWS);
        query.setParam(CommonParams.SORT,orderByClause);
        queryResponse=solrClient.query(collection, query);
        return queryResponse;
	}
	public QueryResponse queryAll(String q,String collection) throws SolrServerException, IOException{
		QueryResponse queryResponse;
		SolrQuery query = new SolrQuery();
		query.setQuery(q).setRows(Integer.MAX_VALUE).setStart(0);
		queryResponse=solrClient.query(collection, query);
		return queryResponse;
	}
	public QueryResponse queryAll(String q,String fq[],String collection) throws SolrServerException, IOException{
		QueryResponse queryResponse;
		SolrQuery query = new SolrQuery();
		query.setQuery(q).setFilterQueries(fq).setRows(Integer.MAX_VALUE).setStart(0);
		queryResponse=solrClient.query(collection, query);
		return queryResponse;
	}
	public QueryResponse query(String q,List<SortClause> value,int pageNo,String collection) throws SolrServerException, IOException{
		QueryResponse queryResponse;
		SolrQuery query = new SolrQuery();
		query.setQuery(q).setSorts(value).setRows(SolrData.QUERY_ROWS).setStart((pageNo-1)*SolrData.QUERY_ROWS);
		queryResponse=solrClient.query(collection, query);
		return queryResponse;
	}
	public QueryResponse queryByRowsSize(String q,List<SortClause> value,Integer rowsSize,String collection) throws SolrServerException, IOException{
		QueryResponse queryResponse;
		SolrQuery query = new SolrQuery();
		query.setQuery(q).setSorts(value).setRows(rowsSize).setStart(0);
		queryResponse=solrClient.query(collection, query);
		return queryResponse;
	}
	public QueryResponse query(String q,int pageNo,String collection) throws SolrServerException, IOException{
        QueryResponse queryResponse;
        SolrQuery query = new SolrQuery();
        query.setQuery(q).setRows(SolrData.QUERY_ROWS).setStart((pageNo-1)*SolrData.QUERY_ROWS);
        queryResponse=solrClient.query(collection, query);
        return queryResponse;
	}
	/**
	 * search with facet
	 * @param q    		查询条件 *:*
	 * @param fq   		查询条件 *:*
	 * @param value 	排序条件 
	 * @param pageSize	结果条数
	 * @param pageNo	页码
	 * @param facet
	 * @param collection集合名称
	 * @return
	 * @throws SolrServerException
	 * @throws IOException
	 */
	public QueryResponse queryWithFacet(String q,String fq[],List<SortClause> sortClauses,int pageNo,Facet facet,String collection) throws SolrServerException, IOException{
        QueryResponse queryResponse;
        SolrQuery query = new SolrQuery();
        
        if(q!=null &&!q.isEmpty()){
        	query.setQuery(q);
        }else{
        	query.setQuery("*:*");
        }
        if(fq!=null&&fq.length>0){
        	query.setFilterQueries(fq);
        }
        if(sortClauses!=null && sortClauses.size()>0){
        	query.setSorts(sortClauses);
        }
        if(pageNo>=0){
        	query.setRows(SolrData.QUERY_ROWS).setStart((pageNo-1)*SolrData.QUERY_ROWS);
        }
        
        if(facet!=null && facet.isStatus()){
        	query.setFacet(true);
        	query.setParam(FacetParams.FACET_FIELD, facet.getFields());
        	
        }
        queryResponse=solrClient.query(collection, query);
        
        return queryResponse;
	}
	/**
	 * 
	 * @param t  			
	 * @param collection	集合名称
	 * @return
	 * @throws IOException
	 * @throws SolrServerException
	 */
	public UpdateResponse add(T t,String collection) throws IOException, SolrServerException{
		UpdateResponse updateResponse;
		updateResponse=solrClient.addBean(collection,t);
		return updateResponse;
	}
	public UpdateResponse addList(List<T> list,String collection) throws IOException, SolrServerException{
		UpdateResponse updateResponse;
		updateResponse=solrClient.addBeans(collection, list);
		return updateResponse;
	}
	/**
	 * 
	 * @param id			
	 * @param collection	集合名称
	 * @return
	 * @throws SolrServerException
	 * @throws IOException
	 */
	public UpdateResponse deleteById(String id,String collection) throws SolrServerException, IOException{
		UpdateResponse updateResponse;
		updateResponse=solrClient.deleteById(collection,id);
		return updateResponse;
	}
	public void rollback(String collection){
		try {
			solrClient.rollback(collection);
		} catch (SolrServerException e) {
			log.error(e.getMessage());
		} catch (IOException e) {
			log.error(e.getMessage());
		}
	}
	public void commit(String collection) throws SolrServerException, IOException{
		solrClient.commit(collection);
	}
	
}
